import base64
import os
import uuid

import git
from django.http import HttpResponse, JsonResponse
from django.shortcuts import render
from dingtalkchatbot.chatbot import DingtalkChatbot

# Create your views here.
from git import Repo
from qiniu import put_data, Auth

from myapp.models import Test
from myapp.utils.upload import qiniu_upload_file


def index(self):
    print(Test.objects.filter(name="123").exists())
    # CURRENT_DIR = os.path.dirname(os.path.abspath(__file__))
    repo = Repo(os.path.join("d:\python/", 'accury'))
    # repo.git.commit(m='提交信息')
    repo.git.pull()
    output = os.popen('service httpd restart')
    print(output.read())

    if (False):
        # WebHook地址
        webhook = 'https://oapi.dingtalk.com/robot/send?access_token=ce0603ad2c8197d7061e79dcd4407fba85b7dcb00783c8d1ec1c1c13b5c7b87d'
        # 初始化机器人小丁
        xiaoding = DingtalkChatbot(webhook)
        # Text消息@所有人
        xiaoding.send_text(msg=CURRENT_DIR)

    return HttpResponse("123")


def year(request):
    years = range(1997, 2018)
    return render(request, 'year_test.html', {"data": years, "tip": "hello world ! "})


def test(request):
    base_64 = request.POST.get('base64')
    print(base_64)
    imgdata = base64.b64decode(base_64)

    # + '.' + file_ext  data:image/png;base64,

    file_name = str(uuid.uuid1()).replace('-', '')
    # url=save_to_local(file,file_name)
    url = qiniu_upload_file(imgdata, file_name)

    return JsonResponse({'url': url})
